package com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it;

import com.glsc.ngateway.common.base.domain.mysql.gateway.other.Fortress;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FortressRepository extends JpaRepository<Fortress, Integer>, JpaSpecificationExecutor<Fortress> {

    @Modifying
    @Query(value = "truncate table t_gw_fortress",nativeQuery = true)
    void trancateTable();

    @Query(value = "select m from Fortress m where 1=1 " +
            "and (:#{#paramSearchDto.account} is null OR m.account LIKE CONCAT('%', :#{#paramSearchDto.account},'%') ) " +
            "and (:#{#paramSearchDto.machine} is null OR m.machine LIKE CONCAT('%',  :#{#paramSearchDto.machine},'%') ) " +
            "and (:#{#paramSearchDto.accountValidityFrom} is null OR m.accountValidityFrom >= :#{#paramSearchDto.accountValidityFrom} ) " +
            "and (:#{#paramSearchDto.accountValidityEnd} is null OR m.accountValidityEnd <= :#{#paramSearchDto.accountValidityEnd}) " +
            "and (:#{#paramSearchDto.status} is null OR m.status= :#{#paramSearchDto.status}) " +
            "and (:#{#paramSearchDto.departmentOneId} is null OR m.departmentOneId= :#{#paramSearchDto.departmentOneId}) " +
            "and (:#{#paramSearchDto.departmentOne} is null OR m.departmentOne LIKE CONCAT('%', :#{#paramSearchDto.departmentOne})) " +
            "and (:#{#paramSearchDto.name} is null OR m.name LIKE CONCAT('%', :#{#paramSearchDto.name})) " +
            "and (:#{#paramSearchDto.systemCode} is null OR m.systemCode = :#{#paramSearchDto.systemCode}) " +
            "")
    Page<Fortress> searchByParam(Pageable pageable, Fortress paramSearchDto);

    List<Fortress> findAllByNotOa(Integer notOa);
}